package com.xianrenit.xianrenpuzi.common.handler.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.Duration;

@Configuration
@EnableCaching //启用自动缓存管理
public class RedisConfig {

    //配置redis键值序列化器
    @Resource
    RedisTemplate redisTemplate;
//    ObjectMapper
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return  new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
                jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance)
                        .serializerByType(Long.TYPE, ToStringSerializer.instance);
            }
        };
    }

    //jdk自带注解： @PostConstruct标注的方法会在 类的对象初始化成功后调用
    @PostConstruct
    public void init(){
        //键：序列化器  一般使用的键都是字符串类型
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //值：序列化器  值会是任意类型的数据  可以使用json序列化器处理
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //hash结构的属性 和值 的序列化器  key  prop  val
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

    }


    //注入redis自动缓存管理器
    @Bean
    public CacheManager redisCacheManager(LettuceConnectionFactory connectionFactory){

        //缓存管理器配置
        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer())) //键序列化器
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new GenericJackson2JsonRedisSerializer())) //值的序列化器
                .entryTtl(Duration.ofMinutes(10)) //缓存过期时间
                //我们配置缓存空值： 为了避免缓存穿透问题- 查询数据库中一定不存在的数据
                //缓存穿透  击穿 雪崩问题
//                .disableCachingNullValues() //不缓存空值数据
                ;
        //获取项目启动时初始化的连接工厂 来构建redis缓存管理器的builder
        RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(configuration)
                .build();

        return redisCacheManager;
    }
}
